const initState = {
  count: 0,
  num: 0,
};

const state = JSON.parse(localStorage.getItem("count_vuex")) || initState;

const mutations = {
  // mutations方法接受state作为第一个参数
  increament(state) {
    state.count++;
    localStorage.setItem("count_vuex", JSON.stringify(state));
  },
  increamentN(state, payload) {
    state.count += payload.n;
    localStorage.setItem("count_vuex", JSON.stringify(state));
  },
};
const actions = {
  incrementWait({ commit }) {
    setTimeout(() => {
      commit("increament");
    }, 1000);
  },
  incrementNWait({ commit }, payload) {
    setTimeout(() => {
      commit("increamentN", payload);
    }, 1000);
  },
};
const getters = {
  // getter其实就是组件中的计算属性,根据state的值衍生出来另外一个值
  // 所以getters中要先拿到sate中的值，getters中第一个参数就是state
  countSqrt(state) {
    return Math.sqrt(state.count);
  },
};
export default {
  // 开启命名空间
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
